//象棋对象
class Chess {
    constructor(opts) {
        this.init(opts);
    }

    //初始化
    init(opts) {
        this.x = opts.x;
        this.y = opts.y;
        this.radius = opts.radius;
        this.color = opts.color;
        this.label = opts.label;
        this.select = opts.select;
        this.type = opts.type;
        this.camp = opts.camp;
        this.active = opts.active;
    }

    //获取棋子数据
    getChessData() {
        return {
            color: this.color,
            label: this.label,
            type: this.type,
            x: this.x,
            y: this.y,
            camp: this.camp,
            select: false,
            active: this.active
        };
    }

    //绘制
    draw(ctx, coordinateX, coordinateY) {
        const {radius, x, y, color, label, select} = this;
        let positionX = coordinateX[x];
        let positionY = coordinateY[y];
        ctx.save();
        //底层阴影
        ctx.save();
        ctx.beginPath();
        ctx.globalAlpha = 0.5;
        ctx.fillStyle = '#555';
        ctx.arc(positionX + 4, positionY + 6, radius, 0, Math.PI * 2);
        ctx.fill();
        ctx.restore();
        //象棋底色圆
        ctx.beginPath();
        ctx.fillStyle = '#ebbc8c';
        ctx.arc(positionX, positionY, radius, 0, Math.PI * 2);
        ctx.fill();
        //线圈
        ctx.beginPath();
        ctx.strokeStyle = color;
        ctx.lineWidth = 2;
        ctx.arc(positionX, positionY - 1, radius - 7, 0, Math.PI * 2);
        ctx.stroke();
        //文字底色圆
        ctx.beginPath();
        ctx.fillStyle = '#eacba8';
        ctx.arc(positionX, positionY - 1, radius - 9, 0, Math.PI * 2);
        ctx.fill();
        //文字
        ctx.fillStyle = color;
        ctx.font = '25px bold serif';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';
        ctx.fillText(label, positionX, positionY - 1);

        if (select) {
            //选中线圈
            ctx.beginPath();
            ctx.strokeStyle = '#409eff';
            ctx.lineWidth = 2;
            ctx.arc(positionX, positionY, radius + 1, 0, Math.PI * 2);
            ctx.stroke();
        }
        ctx.restore();
    }
}

export default Chess;